<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
            <title>Cindy JS</title>

            <script type="text/javascript" src="../build/js/Cindy.js"></script>

            <link rel="stylesheet" href="../css/cindy.css">
            </head>

	<body style="font-family:Arial;">

        <h1>CindyJS: Convex Hull</h1>


        <script id='mouseDrag' type='text/x-cindyscript'>
        if(mover()!=B,

        xx=mouse().x;
yy=mouse().y;
 wy=(startx-xx)*.3;
 wx=-(starty-yy)*.3;

mmmx=[
  [1,0,0],
  [0,cos(wx),sin(wx)],
  [0,-sin(wx),cos(wx)]
];


mmmy=[
  [cos(wy),0,-sin(wy)],
  [0,1,0],
  [sin(wy),0,cos(wy)]
];



  mat=mmmx*mmmy*mat;

          startx=mouse().x;
starty=mouse().y;

);
        </script>

        <script id='mouseUp' type='text/x-cindyscript'>
                if(mover()!=B,


        xx=mouse().x;
yy=mouse().y;
 wy=(startx-xx)*.3;
 wx=-(starty-yy)*.3;

mmmx=[
  [1,0,0],
  [0,cos(wx),sin(wx)],
  [0,-sin(wx),cos(wx)]
];


mmmy=[
  [cos(wy),0,-sin(wy)],
  [0,1,0],
  [sin(wy),0,cos(wy)]
];



  mat=mmmx*mmmy*mat;

  )
        </script>

        <script id='mouseDown' type='text/x-cindyscript'>
                errc("DOWN");
if(|spos0-mouse().xy|<1.5,setstate(0));
if(|spos1-mouse().xy|<1.5,setstate(1));
if(|spos2-mouse().xy|<1.5,setstate(2));
        startx=mouse().x;
starty=mouse().y;
        </script>




<script id='init' type='text/x-cindyscript'>
 firsttime=true;
 setstate(i):=(
 errc("SETSTATE"+i);
     state=i;
     if(state==0,
        obj=obj1;
        calc();repaint();
     );
     if(state==1,
        obj=obj2;
        calc();repaint();
     );
     if(state==2,
        obj=obj3;
        calc();repaint();
     );

 );
state=1;

spos0=(14,7);
spos1=(17,7);
spos2=(20,7);

mat=[
    [1,0,0],
    [0,1,0],
    [0,0,1]
    ];

mat0=[
    [1,0,0],
    [0,1,0],
    [0,0,1]
    ];



mmmx=mat0;
mmmy=mat0;



obj1=((-1,-1,-1),(-1,1,1),(1,-1,1),(1,1,-1))*8;
num=4;





obj2=((1,0,0),(-1,0,0),(0,-1,0),(0,1,0),(0,0,-1),(0,0,1))*12;
num=8;

gg=(1-sqrt(5))/2 ;

obj3=(
      (1,gg,0),
      (1,-gg,0),
      (-1,gg,0),
      (-1,-gg,0),
      (0,1,gg),
      (0,1,-gg),
      (0,-1,gg),
      (0,-1,-gg),
      (gg,0,1),
      (-gg,0,1),
      (gg,0,-1),
      (-gg,0,-1)
      )*8;

      obj=obj2;




ma(p):=[p_1,p_2+2];

pos1=(10,-10,-10);
pos1=pos1/|pos1|;
pos2=(-10,-10,-10);
pos2=pos2/|pos2|;
col1=(1,-10,0);
col2=(0,-10,1);

drawfaceB(f,nn):=(
                  if(area(f_1,f_2,f_3)<0,
                     col=-(pos1*nn*col1+pos2*nn*col2);
                     ff=f++[f_1];
                     apply(1..(length(ff)-1),
                            fillpoly(f,color->col);

                           draw(ff_#,ff_(#+1),size->2,color->(0,0,0))
                           )
                     )
                  );

drawfaceF(f,nn,objc):=(
                  if(area(f_1,f_2,f_3)>0,
                     col=(pos1*nn*col1+pos2*nn*col2);
                     col=objc*(pos1*nn+pos2*nn+.5);
                    // ff=f++[f_1];
                     apply(1..(length(f)),
                           fillpoly(f,color->col,alpha->.5);

                       //    draw(ff_#,ff_(#+1),size->2,color->(0,0,0))
                           )
                     )
                  );


drawfaceFEd(f,nn):=(
                  if(area(f_1,f_2,f_3)>0,
                     col=(pos1*nn*col1+pos2*nn*col2);
                     ff=f++[f_1];
                     apply(1..(length(ff)-1),

                           draw(ff_#,ff_(#+1),size->4,color->(0,0,0))
                           )
                     )
                  );

bary(ch):=(

   barfc=[];
   apply(ch_2,fa,
      p1=(ch_1)_(fa_1);
      p2=(ch_1)_(fa_2);
      p3=(ch_1)_(fa_3);
      errc("****"+p1+" "+p2+" "+p3);
      mid=(p1+p2+p3)/3;
      s12=(p1+p2)/2;
      s23=(p2+p3)/2;
      s31=(p1+p3)/2;
      barfc=barfc++[[p1,s12,mid]];
      barfc=barfc++[[p2,s12,mid]];
      barfc=barfc++[[p2,s23,mid]];
      barfc=barfc++[[p3,s23,mid]];
      barfc=barfc++[[p3,s31,mid]];
      barfc=barfc++[[p1,s31,mid]];

   );
   barfc;

);


calc():=(

ch=convexhull3d(obj);



bar=bary(ch);

);

calc();
        </script>


             <script id='tick' type='text/x-cindyscript'>
             errc("HALLO");

        </script>



<script id='csmove' type='text/x-cindyscript'>
PA=(13,0);
PB=(20,0);
PC=(13,-7);

B.color=(1,1,1);

repeat(10,

if(B.x<PA.x,B.xy=[PA.x+0.001,B.y]);
if(B.y>PA.y,B.xy=[B.x,PA.y-0.001]);

cc=meet(join(PC,PB),perp(B,join(PC,PB)));
dd=meet(join(PC,PA),perp(B,join(PC,PA)));
ee=meet(join(PB,PA),perp(B,join(PB,PA)));


if(area(B,PB,PC)>0 ,
   B.xy=cc.xy+(-.001,0.001);
   if(B.x<PC.x,B.xy=PC+(0.001,0.002));
   if(B.x>K.x,B.xy=PB+(-0.002,-0.001));
);


);

cc=meet(join(PC,PB),perp(B,join(PC,PB)));
dd=meet(join(PC,PA),perp(B,join(PC,PA)));
ee=meet(join(PB,PA),perp(B,join(PB,PA)));

fillpoly([PA,dd,B,ee],color->(.5,.7,0.5));
fillpoly([PB,ee,B,cc],color->(.8,.5,0.5));
fillpoly([PC,cc,B,dd],color->(.5,.5,0.8));

color((0,0,0));
draw(PA,PB,size->3);
draw(PC,PA,size->3);
draw(PC,PB,size->3);

draw(B,cc);
draw(B,dd);
draw(B,ee);

l1=cross(PA,PB);
l1=l1/|(l1_1,l1_2)|;
w1=l1*[B.x,B.y,1]/(-7);
//drawtext((13,5),w1);

l1=cross(PB,PC);
l1=l1/|(l1_1,l1_2)|;
w2=l1*[B.x,B.y,1]/(-7)*sqrt(2);
//drawtext((13,4),w2);

l1=cross(PC,PA);
l1=l1/|(l1_1,l1_2)|;
w3=l1*[B.x,B.y,1]/(-7);
//drawtext((13,3),w3);


A.xy=(0,0);
//translate((2,2));

if(or(mover()==B,firsttime),
errc("CALC");
erg1=apply(bar,b,b_1*w1+b_2*w2+b_3*w3);
if(length(erg>3),firsttime=false;
);
a=convexhull3d(erg1);
);

points=a_1*transpose(mat);
faces=a_2;


apply(faces,f,
      fa=points_f;
      org=(a_1)_f;
      nn=cross( fa_2-fa_1,fa_2-fa_3);
      nn=nn/|nn|;

      mm=cross( org_2-org_1,org_2-org_3);
      mm=mm/|mm|;
      list=apply(obj,|mm*#|);
      objcol=(.5,.5,1);
      if(length(f)==4,
         objcol=(0.5,1,0.5);
      );
      if(length(f)==6,
         objcol=(1,0.5,0.5);
      );
objcol=objcol*.7;
      erg=apply(fa,p,ma(p));




      drawfaceF(erg,nn,objcol);
      );
apply(faces,f,
      fa=points_f;
      nn=cross( fa_2-fa_1,fa_2-fa_3);
      nn=nn/|nn|;
      erg=apply(fa,p,ma(p));


      drawfaceFEd(erg,nn);
      );
drawcircle(spos0,1.2,size->if(state==0,6,2));
drawcircle(spos1,1.2,size->if(state==1,6,2));
drawcircle(spos2,1.2,size->if(state==2,6,2));
drawtext(spos0-(.5,.7),3,size->45);
drawtext(spos1-(.5,.7),4,size->45);
drawtext(spos2-(.5,.7),5,size->45);
        </script>


        <div  id="CSCanvas" style="width:800px; height:600px; border:2px solid #000000"></div>

        <script type="text/javascript">

            var gslp=[
                      {name:"A", kind:"P", type:"Free",alpha:0, pos:[-1000,-1000,1],alpha:.001} ,
                      {name:"B", kind:"P", type:"Free",alpha:0, pos:[15,-3,1]},

                                           ];

            CindyJS({canvasname:"CSCanvas",
                        movescript:"csmove",
                        initscript:"init",
                        mousedownscript:"mouseDown",
                        mouseupscript:"mouseUp",
                        mousedragscript:"mouseDrag",
                        geometry:gslp});


            </script>

       	</body>
</html>
